#include "levelscene.h"
#include "gameconfig.h"
#include "gamecontroller.h"
#include "gamepushbutton.h"

#include <QIcon>
#include <QPixmap>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include <QDebug>
#include <QLabel>


Levelscene::Levelscene(QWidget *parent) : QMainWindow(parent)
{
    this->setWindowTitle("关卡选择");
    this->setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    this->setWindowIcon(QIcon(IMG_GAME_ICON));

    //menu
    QMenuBar *lvMenuBar = this->menuBar();
    QMenu *menuFile = lvMenuBar->addMenu("文件");
    QAction *acBack = menuFile->addAction("返回");
    connect(acBack,&QAction::triggered,this,[=]()
    {
        //返回游戏界面
        GameController::getInstance()->showMianScene();
        GameController::getInstance()->hideLevelScene();
    });

    QAction *acQuit = menuFile->addAction("退出");
    connect(acQuit,&QAction::triggered,this,[=]()
    {
        GameController::getInstance()->hideLevelScene();
        GameController::getInstance()->showGameScene();
    });


    //关卡信息
    for (int i = 0; i<LEVEL_COUNT;i++)
    {
        GamePushButton *btnlevle = new GamePushButton(IMG_LEVEL_ICON,"");
        btnlevle->setParent(this);

        btnlevle->move(30+(i%4)*90,140+(i/4)*70);
        //
        int curlevel = i+1;
        QFont f("宋体",15,Qt::blue);
        QLabel *lblevel = new QLabel(btnlevle);
        lblevel->setGeometry(0,0,btnlevle->width(),btnlevle->height());//因为加入了btn所以，起始位置不再是界面的原点而是btn的原点为此要居中的话直接选择0，0
        lblevel->setAlignment(Qt::AlignCenter);//令label中的文本在label中居中
        lblevel->setFont(f);
        lblevel->setText(QString::number(curlevel));
        //
        connect(btnlevle,&QPushButton::clicked,this,[=]()
        {
         GameController::getInstance()->hideLevelScene();
         GameController::getInstance()->showGameScene(curlevel);
        });


    }






    //返回按钮
    GamePushButton *btnback = new GamePushButton(BMG_BACK_NORMAL,BMG_BACK_PRESSED);
    btnback->setParent(this);
    btnback->move(this->width()- btnback->width(),this->height()- btnback->height());
    connect(btnback,&GamePushButton::clicked,this,[=]()
    {
        GameController::getInstance()->playBackSound();
        GameController::getInstance()->hideLevelScene();
        GameController::getInstance()->showMianScene();
    });


}

void Levelscene::paintEvent(QPaintEvent *event)
{
    QPainter levelPatinter(this);
    QPixmap levelPix;
    bool ret = levelPix.load(IMG_BG_LEVELSCENE);
    if(ret)
    {
        levelPatinter.drawPixmap(0,0,levelPix);
    }

    bool retTitle = levelPix.load(IMG_GAME_TITLE);
    if(retTitle)
    {
        levelPatinter.drawPixmap((GAME_WIDTH-levelPix.width())/2,0,levelPix);
    }

}















